package com.homepig.mongodb;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Filters.lt;
import static com.mongodb.client.model.Updates.combine;
import static com.mongodb.client.model.Updates.currentDate;
import static com.mongodb.client.model.Updates.set;
import static java.util.Arrays.asList;

/**
 * 更新文档
 *
 * @author homepig
 * @version 1.0
 */
public class UpdateDocuments {
    private static MongoCollection mongoCollection;

    static {
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoDatabase mongoDatabase = mongoClient.getDatabase("mongo_stu");
        mongoCollection = mongoDatabase.getCollection("inventory");

        mongoCollection.drop();

        mongoCollection.insertMany(asList(
                Document.parse("{ item: 'canvas', qty: 100, size: { h: 28, w: 35.5, uom: 'cm' }, status: 'A' }"),
                Document.parse("{ item: 'journal', qty: 25, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"),
                Document.parse("{ item: 'mat', qty: 85, size: { h: 27.9, w: 35.5, uom: 'cm' }, status: 'A' }"),
                Document.parse("{ item: 'mousepad', qty: 25, size: { h: 19, w: 22.85, uom: 'cm' }, status: 'P' }"),
                Document.parse("{ item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, status: 'P' }"),
                Document.parse("{ item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, status: 'D' }"),
                Document.parse("{ item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, status: 'D' }"),
                Document.parse("{ item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, status: 'A' }"),
                Document.parse("{ item: 'sketchbook', qty: 80, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"),
                Document.parse("{ item: 'sketch pad', qty: 95, size: { h: 22.85, w: 30.5, uom: 'cm' }, status: 'A' }")
        ));
    }

    /**
     * 更新单个文档
     */
    public void updateASingleDocument() {
        mongoCollection.updateOne(eq("item", "paper"), combine(set("size.uom", "cm"), set("status", "P"),
                currentDate("lastModified")));
    }

    /**
     * 更新多个文档
     */
    public void updateMultipleDocuments() {
        mongoCollection.updateMany(lt("qty", 50), combine(set("size.uom", "in"), set("status", "P"), currentDate("lastModified")));
    }

    /**
     * 更新整个文档
     */
    public void replaceOneDocument() {
        mongoCollection.replaceOne(eq("item", "paper"), Document.parse("{item:'paper',instock:[{warehouse:'B',qty:40},{warehouse:'B',qty:60}]}"));
    }
    public static void main(String[] args) {
        UpdateDocuments updateDocuments = new UpdateDocuments();
        updateDocuments.updateASingleDocument();
        updateDocuments.updateMultipleDocuments();
        updateDocuments.replaceOneDocument();
    }
}
